define(function(){
    var currPageObj={};
    currPageObj.init=function(){
        var self = this;

        queryPageList(1);

        currPageObj.initEvents();
    };

    //绑定事件
    currPageObj.initEvents=function(){
        var self = this;
        $(".page-info").on("click","li",function(){
            var topage = $(this).attr("page");
            if(topage != "#"){
                queryPageList(topage);
            }
        });
        $("#deleteSel").on("click",function(){
            removeSel();
        });

        $("#checkAll").click(function(){
            $(".table_style1 input[type='checkbox']").prop('checked', $("#checkAll").prop('checked'));
        });
        $("#pageSize").on("change",function(){
            queryPageList(1);
        });

        $("#searchBtn").click(function() {
            queryPageList(1);
        });

        $(".operation .operation_add").click(function() {
            showAdd();
        });

        $(".add .cancel").click(function() {
            disshowEdit();
        });

        $(".add .submit").click(function() {
            saveOrEdit();
        });
    };
    return currPageObj;
});

function freshData() {
    var topage = $(".page-info .cur-page").attr("page");
    if(topage != "#" && topage !="undefined"){
        queryPageList(topage);
    }
}

var curPage=0;
var totalPage=0;
function queryPageList(currentpage) {
    var pageSize = $("#pageSize").val();
    var contentName = $("#searchStName").val();
    // var searchStatus = $("#searchStatus").val();
    // var searchType = $("#searchType").val();
    var data = $.ajax({
        type : 'post',
        url : 'content/getContentPageList.htm',
        data : 'contentName=' + contentName+'&currentpage=' + currentpage+'&pageSize='+pageSize,
        cache : false,
        success : function(data) {

            if(data.error){
                return;
            }

            var res =  data.data;
            var pagelist = res.datas;
            curPage = res.pageNumber;
            totalPage = res.totalPages;
            totalCount = res.total;
            var listString = "";
            $("#pageDataList").empty();
            for(var i=0; i<pagelist.length; i++){
                var listItem = pagelist[i];
                var rank = parseInt((curPage-1)*pageSize+i*1+1);
                var stContentId = listItem.stContentId;
                var stContentName = listItem.stContentName;
                var nmOrder = listItem.nmOrder;

                if(i % 2 == 1){
                    listString+="<tr class='even'>";
                }else{
                    listString+=" <tr>";
                }
                listString+="    <td><input name=\"\" type=\"checkbox\" value=\""+stContentId+"\" /></td>";
                listString+="    <td >"+rank+"</td>";
                listString+="    <td><span title="+stContentName+">"+ stContentName+ "</span></td>";
                listString+="    <td><span title="+nmOrder+">"+ nmOrder+ "</span></td>";
                listString+="    <td ><span><a class=\"modify\" onclick=\"showEdit('"+stContentId+"')\" href='javascript:;'>编辑</a>&nbsp;&nbsp;<a class=\"modify\" onclick=\"removeData('"+stContentId+"')\" href='javascript:;'>删除</a></span></td>";
                listString+=" </tr>";
            }
            $("#pageDataList").append(listString);
            $("#totalCount").html(totalCount);
            setPageInfoPart(parseInt(curPage),parseInt(totalPage));
        },
        error : function() {
        }
    });
}

function showAdd(){
    window.location.href ="content/mainpage_add.htm";
}

function showEdit(id) {
    window.location.href ="content/mainpage_add.htm?contentId="+id;
}


function removeData(ids){
    if(confirm("确定要删除吗？")){
        var data = $.ajax({
            type : 'post',
            url : 'content/removeContent.htm',
            data : 'stIds=' + ids,
            cache : false,
            success : function(data) {
                alert(data.msg);
                freshData();
            },
            error : function() {
            }
        });
    }

}

function removeSel(){
    var idList=[];
    $(".table_style1").find('tr :checkbox:checked').each(function(){
        var id = $(this).attr('value');
        idList.push(id);
    });
    if(idList.length>0){
        var ids = idList.join(',');
        //调用删除用户接口
        removeData(ids);
    }
    else{
        alert("至少选择一条数据删除！");
    }
}

function checkInput(){
    var stContentName = $("#stContentName").val();
    if(stContentName.trim() == ""){
        alert("请输入标题！");
        $("#stContentName").focus();
        return false;
    }

    return true;
}

function mouseOver(tip)
{
    $("#"+tip).css("display","inline-block");
}
function mouseOut(tip)
{
    $("#"+tip).css("display","none");
}
//设置页码
function setPageInfoPart(curPage, totalPage) {
    $(".page-info .page").remove();
    var startPage = curPage - 5;//要显示的起始页码
    var endPage = curPage + 5;

    if(startPage<=0){
        startPage = 1;
    }
    if(endPage >= totalPage){
        endPage = totalPage;
    }
    var showPageNum = endPage - startPage+1;//要显示的页码总数
    //添加页码元素
    var lastElem = $(".page-info .pre");
    var count = 0;
    if(startPage>1){
        $(
            "<li page=1 class='page'>1</li>").insertAfter(lastElem);
        lastElem = $(".page-info .page:eq(" + count + ")");
        count++;
    }
    if(curPage - 1>=10){
        $(
            "<li page='#' class=' page '>……</li>").insertAfter(lastElem);
        lastElem = $(".page-info .page:eq(" + count + ")");
        count++;
    }
    for ( var i = count; i < showPageNum+count; i++) {
        if (curPage == startPage) {
            $(
                "<li page=\""+startPage+"\" class=\"page cur-page\">"
                + startPage + "</li>").insertAfter(lastElem);
        } else {
            $(
                "<li page=\""+startPage+"\" class=\"page\">"
                + startPage + "</li>").insertAfter(lastElem);
        }
        lastElem = $(".page-info .page:eq(" + i + ")");
        startPage++;
    }
    count = i;

    if(totalPage - curPage >=10){
        $(
            "<li page='#' class=' page '>……</li>").insertAfter(lastElem);
        lastElem = $(".page-info .page:eq(" + count + ")");
        count++;
    }
    if(endPage<totalPage){
        $(
            "<li page="+totalPage+" class='page'>"+totalPage+"</li>").insertAfter(lastElem);
        lastElem = $(".page-info .page:eq(" + totalPage + ")");
        count++;
    }
    //设置首页，上一页，下一页，最后一页页码
    if(curPage == 1){
        $(".page-info .first").css("display","none");
        $(".page-info .pre").css("display","none");
    }else{
        $(".page-info .first").css("display","inline-block");
        $(".page-info .pre").css("display","inline-block");


        $(".page-info .first").attr("page", 1);
        $(".page-info .pre")
            .attr("page", (curPage - 1) > 0 ? (curPage - 1) : 1);
    }
    if(curPage == totalPage){

        $(".page-info .next").css("display","none");
        $(".page-info .last").css("display","none");
    }else{
        $(".page-info .next").css("display","inline-block");
        $(".page-info .last").css("display","inline-block");
        $(".page-info .next").attr("page",
            (curPage + 1) > totalPage ? totalPage : (curPage + 1));
        $(".page-info .last").attr("page", totalPage);
    }

}

function FormatDate (strTime) {
    if(strTime.trim() == "") return "";
    var date = new Date(Date.parse(strTime.replace(/-/g,"/")));
    return date.Format("yyyy-MM-dd hh:mm:ss");
}


